package models

import "github.com/jinzhu/gorm"

// Component 配件表
type Component struct {
	ID                     string `gorm:"primary_key"`
	No                     string
	Name                   string
	MerchandiseSafetyStock uint
	PackgeNum              uint
	Weight                 float32
	PackgeCube             string
	PackgeWeight           float32
	Logo                   string
	Supply                 string
	Remark                 string
	Location               string
	Number                 uint
	Unit                   string
	PurchasePrice          float32
	StockNumber            float32
}

type ComponentDetail struct {
	ID                     string
	No                     string
	Name                   string
	MerchandiseSafetyStock uint
	PackgeNum              uint
	Weight                 float32
	PackgeCube             string
	PackgeWeight           float32
	Logo                   string
	Supply                 string
	Remark                 string
	Location               string
	Number                 uint
	PurchasePrice          float32
	Shortage               bool
}

// CreateComponent 创建新配件
func CreateComponent(component *Component) error {
	return db.Table("components").Create(component).Error
}

// DeleteComponent 删除配件
func DeleteComponent(componentID string) error {
	return db.Table("components").Where("id = ?", componentID).Delete(Component{}).Error
}

// UpdateComponent 更新配件信息
func UpdateComponent(componentID string, component *Component) error {
	return db.Table("components").Where("id = ?", componentID).Updates(*component).Error
}

// GetComponentsWithPurchasePrice 获取所有配件信息（包含进价）
func GetComponentsWithPurchasePrice() (components []Component, err error) {
	err = db.Table("components").Find(&components).Error
	return
}

// GetComponents 获取所有配件信息
func GetComponents() (components []ComponentDetail, err error) {
	err = db.Table("components").Select(
		"id,no,name, logo,supply,location, number,purchase_price,packge_num,weight,packge_cube,packge_weight,remark,merchandise_safety_stock").Find(&components).Error
	return
}

// GetComponentsStock 获取所有配件信息 // 库存警告那个接口
func GetComponentsStock() (components []ComponentDetail, err error) {
	err = db.Table("components").Select(
		"id,no,name, logo,supply,location, number,purchase_price,packge_num,weight,packge_cube,packge_weight,remark,merchandise_safety_stock").Find(&components).Error

	for index, _ := range components { // TODO 库存警告那个接口
		components[index].Shortage = true
	}
	return
}

// HaveComponent 查询是否有此配件
func HaveComponent(id string) bool {
	return !db.Table("components").Where("id = ?", id).First(&Component{}).RecordNotFound()
}

// GetComponent 获取配件信息
func GetComponent(id string) *Component {
	var component Component
	db.Table("components").Where("id = ?", id).Select(
		"id,no, name, logo,supply,location, number,purchase_price,packge_num,weight,packge_cube,packge_weight,remark,merchandise_safety_stock").First(&component)
	return &component
}

func getComponentNumber(tx *gorm.DB, goodsID string) (number uint, err error) {
	info := struct {
		Number uint
	}{}
	err = tx.Table("components").Select("number").Where("id = ?", goodsID).Scan(&info).Error
	number = info.Number
	return
}

func updateComponentNumber(tx *gorm.DB, goodsID string, number uint) error {
	return tx.Table("components").Where("id = ?", goodsID).Update("number", number).Error
}

// 重置配件总数
func Reset(components []ComponentDetail) map[string]uint {
	componentMap := map[string]uint{}
	if components != nil {
		for _, component := range components {
			componentMap[component.ID] = component.Number
		}
	}
	return componentMap

}
